package com.fili.resources.texture;


import com.fili.resources.FileSystem;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.ComponentColorModel;
import java.awt.image.DataBuffer;
import java.io.IOException;
import java.util.HashMap;

/**
 * Created by Claudiu-Florin FILIMON
 * Date: Nov 3, 2006
 * Time: 5:12:35 PM
 * Revision: 0
 */
public class TextureFactory
{
    private HashMap textures = null;
    private static BufferedImage emptyImage = null;
    private static Color transparentColor = null;
    private static Color opaqueColor = null;


    private static BufferedImage getEmptyImage()
    {
        if (emptyImage == null)
        {
            emptyImage = new BufferedImage(32, 32, BufferedImage.TYPE_INT_RGB);
            Graphics g = emptyImage.getGraphics();
            for (int i = 0; i < emptyImage.getHeight(); i += 4)
                for (int j = 0; j < emptyImage.getWidth(); j += 4)
                {
                    if ((i / 4 + j / 4) % 2 == 0)
                        g.setColor(Color.yellow);
                    else
                        g.setColor(Color.red);
                    g.fillRect(j, i, 4, 4);
                }
        }

        return emptyImage;
    }


    public static Color getTransparentColor()
    {
        if (transparentColor == null)
            transparentColor = new Color(0x00000000);
        return transparentColor;
    }


    public static Color getOpaqueColor()
    {
        if (opaqueColor == null)
            opaqueColor = new Color(0xffffffff);
        return opaqueColor;
    }

    public static Texture getTexture(String path)
    {
        Texture tex;
        BufferedImage bi;

        try
        {
            bi = ImageIO.read(FileSystem.getStream(path));
        }
        catch (IOException e)
        {
            bi = getEmptyImage();
        }

        tex = new Texture(bi);

        return tex;
    }


}
